package com.tyk.bookstore.front.message.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tyk.bookstore.front.common.core.util.AssertUtil;
import com.tyk.bookstore.front.message.model.entity.ServiceNoticeDo;
import com.tyk.bookstore.front.message.model.query.ServiceNoticeListQuery;
import com.tyk.bookstore.front.message.model.query.ServiceNoticePageQuery;
import com.tyk.bookstore.front.message.model.result.ServiceNoticeResultEnum;
import com.tyk.bookstore.front.message.model.vo.ServiceNoticeListVo;
import com.tyk.bookstore.front.message.model.vo.ServiceNoticePageVo;
import com.tyk.bookstore.front.message.model.vo.ServiceNoticeVo;
import com.tyk.bookstore.front.message.repository.ServiceNoticeRepository;
import com.tyk.bookstore.front.message.service.ServiceNoticeService;
import com.tyk.bookstore.front.message.util.MapStructMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ServiceNoticeServiceImp implements ServiceNoticeService {
    private final ServiceNoticeRepository serviceNoticeRepository;
    private final MapStructMessage mapStructMessage;

    @Override
    public ServiceNoticeVo getServiceNoticeById(Long id) {
        ServiceNoticeDo notice = serviceNoticeRepository.selectById(id);
        AssertUtil.notNull(notice, ServiceNoticeResultEnum.NOTICE_NOT_FOUND);
        return mapStructMessage.toServiceNoticeVo(notice);
    }

    @Override
    public List<ServiceNoticeListVo> getList(ServiceNoticeListQuery query) {
        return serviceNoticeRepository.selectServiceNoticeSimList(query.getSize());
    }

    @Override
    public ServiceNoticePageVo getPage(ServiceNoticePageQuery query) {
        Page<ServiceNoticeDo> page = new Page<>(query.getPage(), query.getLimit());
        LambdaQueryWrapper<ServiceNoticeDo> w = Wrappers.lambdaQuery();
        serviceNoticeRepository.selectPage(page, w);
        return mapStructMessage.toServiceNoticePageVo(page);
    }
}
